// Package ioc
// Created by GoLand
// @User: lenora
// @Date: 2024/7/16
// @Time: 15:34

package ioc

import (
	"camp/microbook/interactive/events"
	"github.com/IBM/sarama"
	"github.com/spf13/viper"
)

func InitSaramaClient() sarama.Client {
	type kafkaConf struct {
		Addr []string `yaml:"addr"`
	}
	var cfg kafkaConf
	err := viper.UnmarshalKey("kafka", &cfg)
	if err != nil {
		panic(err)
	}
	saramaCfg := sarama.NewConfig()
	saramaCfg.Producer.Return.Successes = true
	client, err := sarama.NewClient(cfg.Addr, saramaCfg)
	if err != nil {
		panic(err)
	}
	return client
}

func InitSyncProducer(client sarama.Client) sarama.SyncProducer {
	producer, err := sarama.NewSyncProducerFromClient(client)
	if err != nil {
		panic(err)
	}
	return producer
}

func InitConsumers(c1 *events.InteractiveReadConsumer) []events.Consumer {
	return []events.Consumer{c1}
}
